use yt_dlp::Youtube;
use std::path::PathBuf;

#[tokio::main]
pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let executables_dir = PathBuf::from("libs");
    let output_dir = PathBuf::from("output");

    let mut fetcher = Youtube::with_new_binaries(executables_dir, output_dir).await?;
    fetcher.with_timeout(tokio::time::Duration::from_secs(60));

    // Download a video
    let url = String::from("https://www.youtube.com/shorts/Zx8sPApAhYA");
    // let url = String::from("https://www.youtube.com/shorts/E7KOb6eILjg");
    let video = fetcher.fetch_video_infos(url).await?;

    let audio_format = video.best_audio_format().unwrap();
    let audio_path = fetcher.download_format(&audio_format, "audio-stream.mp3").await?;

    let video_format = video.best_video_format().unwrap();
    let video_path = fetcher.download_format(&video_format, "video-stream.mp4").await?;

    let output_path = fetcher.combine_audio_and_video("audio-stream.mp3", "video-stream.mp4", "my-output.mp4").await?;

    // 这样下载会包含字幕
    /*let url = String::from("https://www.youtube.com/shorts/Zx8sPApAhYA");
    let video_path = fetcher.download_video_from_url(url, "my-video.mp4").await?;*/

    Ok(())
}
